(function() {
	"use strict";
	var $findForm;
	
	var $listResultEL;
	var $selectAllBtn;
	var $addForm;
	var $ruZhuBtn;
	var $deleteBtn;
	
	$(function() {
		
		$listResultEL = $("#listResultEL");
		$findForm = $("#findForm");
		
		$selectAllBtn = $("#selectAllBtn");
		$addForm = $("#addForm");
		$ruZhuBtn = $("#ruZhuBtn");
		
		$ruZhuBtn.click(handleruZhuBtnClick);
		$selectAllBtn.click(handleselectAllBtnClick).click();
		
	

	});
	
	
	
	// 新增
	var handleruZhuBtnClick = function(evt) {

		evt.preventDefault();

		var url = "/beadhouse/api/customer/addCustomer";
		$.get(url, $addForm.serialize(), function(resp) {

			if (resp.code === "ok") {
				console.log(resp);
				alert("新增成功");
				
			} else {
				alert(resp.message);
			}
		})
	};
	// 查询列表
	var handleselectAllBtnClick = function(evt) {

		evt.preventDefault();

		var url = "/beadhouse/api/customer/selectAll";
		$.get(url, $findForm.serialize(), function(resp) {
				
			if (resp.code === "ok") {
				var rows = resp.data;
				console.log(resp.data);
				if (rows.length > 0) {
					paintResult(rows);
				} else {
					paintNoResult();
				}
			}
		})
	};
	//删除
	window.handleDeleteBtnClick = function(id) {
		
		var url = "/beadhouse/api/customer/delete?id="
				+ id;
		$.get(url, function(resp) {

			if (resp.code === "ok") {
				console.log(resp);
				alert("删除成功");

			} else {
				alert(resp.message);
			}
		})
	};
	
	

	// 表格
	var paintResult = function(rows) {
		var html = "";
		for (var i = 0, len = rows.length; i < len; i++) {
			var row = rows[i];
			html += '<tr>';
			html += '<td>' + (i + 1) + '</td>';
			html += '<td>' + row.customerName+ '</td>';
			html += "<td>" + row.customerAge + "</td>";
			if(row.customerSex){
				html += "<td>  男 </td>"
			}else{
				html += "<td>  女 </td>"
			}
		
			
			html += "<td>" + row.idcard + "</td>"
			html += "<td>" + row.roomNumber + "</td>"
			html += "<td>" + row.recordId + "</td>"
			html += "<td>" + row.checkinDate + "</td>"
			html += "<td>" + row.expirationDate + "</td>"
			html += "<td>" + row.contactTel + "</td>"
			html += "<td>" + row.remark + "</td>"
			html += '<td>';
			html += '<a href="page/rzdjxiuGai.html#'+row.id+'" >查看</a>';
			html += "</td>";
			html += '<td>';
			html += "<button  onclick='handleDeleteBtnClick("+row.id+")' > 删除</button>";
			html += "</td>";
			html += "</tr>";
		}
		$listResultEL.html(html);

	};
	
	var paintNoResult = function() {
		var html = "<tr><td colspan='99999'>";
		html += "很抱歉，未找到相关数据";
		html += "</td></tr>"
		$listResultEL.html(html);
	};
	
	
	
})();
